package com.sztx.pay.center.worker.quartz;

import java.math.BigDecimal;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.sztx.bbmall.sms.facade.SmsFacade;
import com.sztx.bbmall.sms.proj.SmsSendTemplateRequest;
import com.sztx.pay.center.common.util.Constance;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.CurrencyUtil;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActAlertMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.UserActAlertDO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.se.core.quartz.job.BaseJob;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;

@Component("userAlertJob")
public class UserAlertJob extends BaseJob{
	protected final Logger LOG = LoggerFactory.getLogger(UserAlertJob.class);
	@Autowired
	UserActAlertMysqlDAO userActAlertMysqlDAO;
	
	@Autowired
	private ActService actService;
	
	@Autowired(required = true)
	private DubboClientFactory dubboClientFactory;
	@Override
	public Object executeJob(JobExecutionContext context)
			throws JobExecutionException {
		LOG.info("----------------UserAlertJob start----------------");
		UserActAlertDO userActAlertDO = new UserActAlertDO();
		userActAlertDO.setState(1);
		List<UserActAlertDO> userActAlertList = userActAlertMysqlDAO.listPage(userActAlertDO , null);

		
		for (UserActAlertDO userActAlert : userActAlertList) {
			String userId = userActAlert.getUserId();
			int actType = userActAlert.getActType();
			
			UserActType userActType = UserActType.valueOf(actType);
			UserActDO userActDO = actService.findUserAct(userId, userActType);
			if(userActDO != null){
				Integer alertTimes = userActAlert.getAlertTimes();
				Integer alertTotalTimes = userActAlert.getAlertTotalTimes();
				Long alertBalance = userActAlert.getAlertBalance();
				Long actBalance = userActDO.getActBalance();
				
				UserActAlertDO updateActAlertDO = new UserActAlertDO();
				updateActAlertDO.setUserId(userId);
				updateActAlertDO.setActType(userActAlert.getActType());
				if(alertBalance > actBalance){
					//需要进行预警
					if(Constance.maxAlertTimes <= alertTimes){
						//超过最大预警次数
						LOG.info("账户【"+userId+"】预警次数超过最大值"+Constance.maxAlertTimes);
						continue;
					}
					//更新alert_times alert_total_times
					updateActAlertDO.setAlertTimes(alertTimes + 1);
					updateActAlertDO.setAlertTotalTimes(alertTotalTimes + 1);
					userActAlertMysqlDAO.update(updateActAlertDO);
					notifyUser(userActAlert, actBalance);
				}else if(actBalance > 0 && alertTimes > 0){
					//清空预警次数
					updateActAlertDO.setAlertTimes(0);
					userActAlertMysqlDAO.update(updateActAlertDO);
				}
			}
			
		}
		LOG.info("----------------UserAlertJob end----------------");
		return null;
	}
	protected SmsFacade getSmsFacade() {
		return dubboClientFactory.getDubboClient("smsFacadeDubboClient");
	}
	//短信通知用户
	@Async
	private void notifyUser(UserActAlertDO userActAlertDO,long banlance){
		//发送短信
		String alertMobile = userActAlertDO.getAlertMobile();
		String[] splitMobile = alertMobile.split(",");
		SmsSendTemplateRequest request = new SmsSendTemplateRequest();
		request.setInstallId(Constance.DEFAULT_PLATFORMID);
		
		BigDecimal userBanlance = CurrencyUtil.fen2yuan(banlance);
		BigDecimal alertBanlance = CurrencyUtil.fen2yuan(userActAlertDO.getAlertBalance());
		
		
		String[] templateArray = new String[] { userBanlance.toString(),alertBanlance.toString() };
		request.setTemplateArray(templateArray);
		request.setCmd(Constance.USER_ACT_ALERT_CMD);
		for (String mob : splitMobile) {
			request.setPhone(mob);
			getSmsFacade().SendTemplate(request);
		}
	}
}
